/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package libraries;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.Object;

/**
 *
 * @author aeguzkiza
 */
public class SystemCall {

    public static Object[] SystemCall(String call){
        boolean error = false;
        Runtime r = Runtime.getRuntime();
        String buffer = null;
        try{
            System.out.println("Executing systemCall... ["+call+"]");
            Process p = r.exec("CMD /c"+call);
            InputStream in = p.getInputStream();
            BufferedInputStream buf = new BufferedInputStream(in);
            InputStreamReader inread = new InputStreamReader(buf);
            BufferedReader bufferedreader = new BufferedReader(inread);

            // Read the ls output
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                buffer = buffer+"\n"+line;
                //System.out.println(line);
            }
            try {
                if (p.waitFor() != 0) {
                    error = true;
                    System.err.println("exit value = " + p.exitValue());
                    //throw new IllegalArgumentException("The systemCall couldn't be done: [" +call+"]");
                }
            } catch (InterruptedException e) {
                error = true;
                System.err.println(e);
                //throw new IllegalArgumentException("The systemCall couldn't be done: [" +call+"]");
            } finally {
                // Close the InputStream
                bufferedreader.close();
                inread.close();
                buf.close();
                in.close();
            }
        }
        catch (IOException e){
            System.err.println(e.getMessage());
        }
        Object[] result = {error,buffer};
        return result;
    }

}
